import numpy as np
from app.services.prediction_parameters.load_models import model_manager
from app.services.prediction_parameters.model_utils import predict_next_n_hours  # 假设你放predict函数那里

def do_prediction_for_var(var_name: str, input_data: np.ndarray, predict_hours=24):
    """
    var_name: 'load_kW' 或 'grid_price' 等四个键之一
    input_data: numpy数组，形状 (至少24, 1)，未归一化的历史数据
    predict_hours: 预测未来小时数
    """
    if var_name not in model_manager.models or var_name not in model_manager.scalers:
        raise ValueError(f"模型或Scaler未加载: {var_name}")

    model = model_manager.models[var_name]
    scaler = model_manager.scalers[var_name]

    preds = predict_next_n_hours(model, scaler, input_data, n_hours=predict_hours)
    return preds


# 普通调用示例
async def example():
    # 确保model_manager.load_all() 已经被调用（一般在启动时）
    # 这里模拟输入数据，比如24小时负荷数据
    base_load = 120
    peak_load = 80
    hours = np.arange(24)
    daily_pattern = peak_load * np.sin((hours - 6) / 24 * 2 * np.pi) + peak_load
    noise = np.random.normal(0, 5, size=24)
    input_data = base_load + daily_pattern + noise
    input_data = input_data.reshape(-1, 1)

    preds_load = do_prediction_for_var("load_kW", input_data, predict_hours=24)
    print("未来24小时负荷预测：", preds_load)
